<template>
    <table border="1">
        <tr>
            <td>门店名称</td>
            <td>
                <input type="text" v-model="info.sname">
            </td>
        </tr>
        <tr>
            <td>门店地址</td>
            <td>
                <input type="text" v-model="info.saddr">
            </td>
        </tr>
        <tr>
            <td>门店图片</td>
            <td>
                <img :src="info.spicture" style="width: 120px;height: 110px;">
                <input type="file" @change="upload">
            </td>
        </tr>

        <tr>
            <td>门店介绍</td>
            <td>
                <input type="text" v-model="info.sintro">
            </td>
        </tr>
        <tr>
            <td>服务范围</td>
            <td>
                <select v-model="info.said">
                    <option :value="-1">--请选择--</option>
                    <option v-for="item in bindt" :value="item.said">{{item.sarea}}</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>店主</td>
            <td>
                <input type="text" v-model="info.owner">
            </td>
        </tr>
        <tr>
            <td>联系电话</td>
            <td>
                <input type="text" v-model="info.sphone">
            </td>
        </tr>
        <tr>
            <td>营业时间</td>
            <td>
                <input type="date" v-model="info.servicetime">
            </td>
        </tr>
        <tr>
            <td>门店状态</td>
            <td>
                <input type="radio" :value="true" v-model="info.shopstate">显示
                <input type="radio" :value="false" v-model="info.shopstate">隐藏
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: center;">
                <input type="button" value="保存" @click="add">
            </td>
        </tr>
    </table>
</template>
<script setup lang="ts">
import {ref} from 'vue'
import axios from 'axios';
import { onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { useRoute } from 'vue-router';
onMounted(()=>{
    bind();
    fantian();
})
const upload=(x:any)=>{
    var file=x.target.files[0];
    var fd=new FormData();
    fd.append("files",file);
    axios.post('http://localhost:63668/api/UpFile',fd)
    .then(res=>{
        info.value.spicture=res.data
    })
    .catch(error=>{
        console.log(error);
    })
}
const rou=useRoute();
const fantian=()=>{
    axios.get('http://localhost:63668/api/Shop/Fill',{
        params:{
            id:rou.params.id
        }
    })
    .then(res=>{
        res.data.servicetime=res.data.servicetime.substring(0,10)
        info.value=res.data
    })
    .catch(error=>{
        console.log(error);
    })
}
const bindt=ref([{
    "said": 0,
    "sarea": ""
}])
const bind=()=>{
    axios.get('http://localhost:63668/api/Shop/Bind_s')
    .then(res=>{
        bindt.value=res.data
    })
    .catch(error=>{
        console.log(error);
    })
}
const info=ref({
    "sid": 0,
    "said": "",
    "sname": "",
    "spicture": "",
    "sphone": "",
    "servicetime": "",
    "sintro": "",
    "shopstate": true,
    "saddr": "",
    "owner": "",
    "sisdel": 0
})
const ror=useRouter();
const add=()=>{
    if(info.value.sname==""){
        alert('门店名称不能为空');
        return;
    }
    if(info.value.servicetime==""){
        alert('门店营业时间不能为空');
        return;
    }
    if(info.value.said==""){
        alert('服务范围不能为空');
        return;
    }
    if(info.value.saddr==""){
        alert('门店地址不能为空');
        return;
    }
    if(info.value.shopstate==null){
        alert('门店状态不能为空');
        return;
    }
    axios.post('http://localhost:63668/api/Shop/UpdShop',info.value)
    .then(res=>{
        if(res.data>0){
            alert('修改成功');
            ror.push('/ShowShop');
        }
        else{
            alert('修改失败');
        }
    })
    .catch(error=>{
        console.log(error);
    })
}
</script>